package com.xii.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xii.eduservice.entity.EduCourse;
import com.xii.eduservice.entity.EduCourseDescription;
import com.xii.eduservice.entity.frontvo.CourseQueryVo;
import com.xii.eduservice.entity.frontvo.CourseWebVo;
import com.xii.eduservice.entity.vo.CourseInfoForm;
import com.xii.eduservice.entity.vo.CoursePublishVo;
import com.xii.eduservice.mapper.EduCourseMapper;
import com.xii.eduservice.service.EduChapterService;
import com.xii.eduservice.service.EduCourseDescriptionService;
import com.xii.eduservice.service.EduCourseService;
import com.xii.eduservice.service.EduVideoService;
import com.xii.servicebase.exceptionhandler.XiiException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xhjava
 * @since 2022-02-13
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //用于添加描述
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;


    /**
     * @description: 添加课程
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse); //将封装的对象courseInfoForm 里面的数据赋值eduCourse  eduCourse用于添加
        int insert = baseMapper.insert(eduCourse);
//        this.save(eduCourse);  添加也可以使用这个
        if(insert == 0){
            throw new XiiException(20001,"添加课程失败");
        }

        //向课程简介表中添加课程描述  -使id值与课程id相同
        String id = eduCourse.getId();

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(id);
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescriptionService.save(eduCourseDescription);

        return id;
    }

    /**
     * @description: 根据ID查询课程
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @Override
    public CourseInfoForm getCourseInfoFormById(String id) {
        // 根据课程id课程信息
        EduCourse course = this.getById(id);
        if(course == null){
            throw new XiiException(20001, "数据不存在");
        }
        //创建返回的对象
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        //课程信息存入返回对象
        BeanUtils.copyProperties(course, courseInfoForm);

        // 根据课程id课程描述信息
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(id);
        if(course != null){
            //描述信息存入返回对象
            courseInfoForm.setDescription(courseDescription.getDescription());
        }
        return courseInfoForm;
    }

    /**
     * @description: 更新课程
     * 
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @Override
    public void updateCourseInfoById(CourseInfoForm courseInfoForm) {
        //保存课程基本信息
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, course);
        //修改课程表
        boolean resultCourseInfo = this.updateById(course);
        if(!resultCourseInfo){
            throw new XiiException(20001, "课程信息保存失败");
        }
        //保存课程描述信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(course.getId());
        //修改描述表
        boolean resultDescription = eduCourseDescriptionService.updateById(courseDescription);
        if(!resultDescription){
            throw new XiiException(20001, "课程详情信息保存失败");
        }

    }

    /**
     * @description: 根据课程id得到将要发布的课程信息
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @Override
    public CoursePublishVo getPublishCourseInfoFormById(String courseId) {
        CoursePublishVo coursePublishVo = baseMapper.getCoursePublishVo(courseId);
        return coursePublishVo;
    }

    //删除课程
    @Override
    public void removeCourse(String courseId) {
        //1 根据课程id删除小节
        eduVideoService.removeVideoByCourseId(courseId);

        //2 根据课程id删除章节
        eduChapterService.removeChapterByCourseId(courseId);

        //3 根据课程id删除描述
        eduCourseDescriptionService.removeById(courseId);

        //4 根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if(result == 0) { //失败返回
            throw new XiiException(20001,"删除失败");
        }
    }

    /**
     * @description: 查询course列表返回到courseQuery 并分页
     *
     * @return {@link null}
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @Override
    public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }
        baseMapper.selectPage(pageParam, queryWrapper);
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    /**
     * 用户端课程信息
     * @param courseId
     * @return
     */
    @Override
    public CourseWebVo selectInfoWebById(String courseId) {
//        this.updatePageViewCount(id);
        //根据课程id查询基本信息
        return baseMapper.selectInfoWebById(courseId);
    }
}
